package com.example.contentobserverdb;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
public class PersonalProvider extends ContentProvider {
    //定义一个uri路径匹配器，如果路径匹配不成功，则返回-1
    private static UriMatcher mUriMatcher=new UriMatcher(-1);
    private static final int SUCCESS=1;      //定义匹配路径成功时的返回码
    private PersonalDBOpenHelper helper;     //声明数据库帮助类对象
    //静态代码块，添加Uri路径匹配器规则，当路径是"content://com.example.contentobserverdb/info"时会匹配成功
    static {
        mUriMatcher.addURI("com.example.contentobserverdb","info",SUCCESS);
    }
    public PersonalProvider() {
    }

    @Override
    public boolean onCreate() {   //当内容提供者被创建时调用
        helper=new PersonalDBOpenHelper(getContext());   //实例化数据库帮助类对象
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        //匹配查询的uri路径
        int code=mUriMatcher.match(uri);
        if(code==SUCCESS){
            SQLiteDatabase db=helper.getReadableDatabase();
            return db.query("info",projection, selection, selectionArgs, null, null, sortOrder);
        }else{
            throw new IllegalArgumentException("路径不正确，无法查询数据！");
        }
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        int code=mUriMatcher.match(uri);
        if(code==SUCCESS){
            SQLiteDatabase db=helper.getWritableDatabase();
            long rowId=db.insert("info",null,values);
            if(rowId>0) {
                Uri insertedUri = ContentUris.withAppendedId(uri, rowId);
                //提示数据库的内容变化了
                getContext().getContentResolver().notifyChange(insertedUri, null);
                return insertedUri;
            }
            db.close();
            return uri;
        }else {
            throw new UnsupportedOperationException("路径不正确，无法添加数据！");
        }
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int code=mUriMatcher.match(uri);
        if(code==SUCCESS){
            SQLiteDatabase db=helper.getWritableDatabase();
            int count=db.delete("info",selection,selectionArgs);
            //提示内容变化了
            if(count>0){
                getContext().getContentResolver().notifyChange(uri,null);
            }
            db.close();
            return count;
        }else{
        throw new UnsupportedOperationException("路径不正确，无法删除数据！");
        }
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        int code=mUriMatcher.match(uri);
        if(code==SUCCESS){
            SQLiteDatabase db=helper.getWritableDatabase();
            int count=db.update("info",values,selection,selectionArgs);
            //提示数据库内容变化了
            if(count>0){
                getContext().getContentResolver().notifyChange(uri,null);
            }
            db.close();
            return count;
        }else {
            throw new UnsupportedOperationException("路径不正确，无法更新数据！");
        }
    }
}
